package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.SystemMenu;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Iterator;
import java.util.List;

public class SystemMenuUtil {

    public static final String SYSTEM_MENU_IN_SESSION = "system_menu_in_session";

    public static void fitlerSystemMenu(List<SystemMenu> menus){
        Subject subject = SecurityUtils.getSubject();

        Iterator<SystemMenu> iter = menus.iterator();
        while(iter.hasNext()){
            SystemMenu menu = iter.next();
            Permission permission = menu.getPermission();
            if(permission != null){
                if(!subject.isPermitted(permission.getResource())){
                    iter.remove(); //删除
                }
            }
            List<SystemMenu> children = menu.getSubset();
            if(children != null && children.size() > 0){
                fitlerSystemMenu(children);
            }
        }
    }
}
